package com.example.administrator.zhongyichou.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;

import com.example.administrator.zhongyichou.Bean.BaseEventBean;
import com.example.administrator.zhongyichou.R;
import com.example.administrator.zhongyichou.adapter.MillionsJoinAdapter;
import com.example.administrator.zhongyichou.base.BaseActivity;
import com.example.administrator.zhongyichou.base.MyApplication;
import com.example.administrator.zhongyichou.net.NetConfig;
import com.example.administrator.zhongyichou.net.core.HttpLoader;
import com.example.administrator.zhongyichou.net.core.RequestHttpLoader;
import com.example.administrator.zhongyichou.net.protocol.GsonBaseProtocol;
import com.example.administrator.zhongyichou.net.protocol.MillionsJoinProtocol;
import com.example.administrator.zhongyichou.view.HeaderView;

import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by lichenyu on 2017/12/8.
 * 描述：百万行动加入
 */

public class MillionsJoinsActivity extends BaseActivity<MillionsJoinProtocol> implements View.OnClickListener, AdapterView.OnItemClickListener {
    @BindView(R.id.headerview)
    HeaderView headerview;
    @BindView(R.id.txt_count)
    TextView txtCount;
    @BindView(R.id.listview)
    ListView listview;
    @BindView(R.id.btn_join)
    Button btnJoin;
    private Button btnAdd;
    private MillionsJoinProtocol millionsJoinProtocol;
    private MillionsJoinAdapter adapter;

    @Override
    protected void init() {
        ButterKnife.bind(this);
        initHead();
        View foot = LayoutInflater.from(this).inflate(R.layout.millions_join_list_foot, null);
        btnAdd = (Button) foot.findViewById(R.id.btn_add);
        btnAdd.setOnClickListener(this);
        listview.addFooterView(foot);
        listview.setOnItemClickListener(this);
    }

    private void initHead() {
        headerview.setTitle("加入行动");
        headerview.bindOnclickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                switch (v.getId()) {
                    case HeaderView.LEFT:
                        finish();
                        break;
                }
            }
        });
    }

    @Override
    protected void responseData(MillionsJoinProtocol mGsonBaseProtocal) {
        millionsJoinProtocol = mGsonBaseProtocal;
        adapter = new MillionsJoinAdapter(this, millionsJoinProtocol.getData());
        listview.setAdapter(adapter);
    }

    @Override
    protected HttpLoader initHttpLoader() {
        RequestHttpLoader requestHttpLoader = new RequestHttpLoader(MillionsJoinProtocol.class);
        Map map = new HashMap();
        map.put("user_id", MyApplication.getUserID());
        requestHttpLoader.setParams(NetConfig.ACCESS_MUTUAL, map);
        return requestHttpLoader;
    }

    @Override
    protected boolean isRequestData() {
        return true;
    }

    @Override
    protected int initContentView() {
        return R.layout.millions_join_activity;
    }

    @Override
    public boolean onEventBus(BaseEventBean baseEventBean) {
        List<MillionsJoinProtocol.DataBean> list = (List<MillionsJoinProtocol.DataBean>) baseEventBean.data;
        millionsJoinProtocol.getData().addAll(list);
        adapter.notifyDataSetChanged();
        return false;
    }

    @OnClick(R.id.btn_join)
    public void onViewClicked() {
        StringBuffer sb = new StringBuffer();
        for (MillionsJoinProtocol.DataBean dataBean : millionsJoinProtocol.getData()) {
            if (dataBean.isSelect()) {
                sb.append(dataBean.getMember_id() + ",");
            }
        }
        Intent intent = new Intent(this, WaitPayActivity.class);
        intent.putExtra("mutual_type", 1);
        intent.putExtra("member_id", sb.toString());
        startActivity(intent);
    }

    @Override
    public void onClick(View v) {
        Intent intent = new Intent(this, JoinActionActivity.class);
        intent.putExtra("mutual_type", 1);
        startActivity(intent);
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        millionsJoinProtocol.getData().get(position).setSelect(!millionsJoinProtocol.getData().get(position).isSelect());
        adapter.notifyDataSetChanged();
    }
}
